package com.atguigu.yygh.apartment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.apartment.repository.StudentInfoRepository;
import com.atguigu.yygh.apartment.service.ApartmentService;
import com.atguigu.yygh.apartment.service.ChamberService;
import com.atguigu.yygh.apartment.service.StudentInfoService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.hosp.Chamber;
import com.atguigu.yygh.model.hosp.StudentInfo;
import com.atguigu.yygh.model.hosp.StudentInfo;
import com.atguigu.yygh.model.user.StudentDetail;
import com.atguigu.yygh.vo.hosp.StudentInfoQueryVo;
import com.atguigu.yygh.vo.user.UserInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import com.atguigu.yygh.apartment.config.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StudentInfoServiceImpl implements StudentInfoService {

    @Autowired
    private StudentInfoRepository studentInfoRepository;

    @Autowired
    private ApartmentService apartmentService;

    @Autowired
    private ChamberService chamberService;

    @Override
    public void save(Map<String, Object> paramMap) {
        System.out.println("paramMap:"+paramMap.toString());
        //把map集合转换成对象 StudentInfo
        String mapString = JSONObject.toJSONString(paramMap);
        StudentInfo studentInfo = JSONObject.parseObject(mapString, StudentInfo.class);
        System.out.println("studentInfo_id:"+studentInfo.getId());
        studentInfo.setId(studentInfo.getBedId());
        //判断是否存在数据
        StudentInfo studentInfoExist = studentInfoRepository.getStudentInfoByApartmentCodeAndChamberCodeAndBedId(
                studentInfo.getApartmentCode(),studentInfo.getChamberCode(),studentInfo.getBedId()
        );
        //如果不存在，进行添加
        //存在，修改
        if (studentInfoExist!=null) {
            studentInfoExist.setStatus(1);
            studentInfoExist.setUpdateTime(new Date());
            studentInfoExist.setAmountStatus(1);
            studentInfoExist.setIsDeleted(0);
            studentInfoExist.setTitle(studentInfo.getTitle());
            studentInfoRepository.save(studentInfo);
        }else {
            studentInfo.setStatus(1);
            studentInfo.setCreateTime(new Date());
            studentInfo.setUpdateTime(new Date());
            studentInfo.setAmountStatus(1);
            studentInfo.setIsDeleted(0);
            studentInfoRepository.save(studentInfo);
        }

    }

    @Override
    public void save(StudentInfo studentInfo) {
        studentInfo.setId(studentInfo.getBedId());
        //判断是否存在数据
        StudentInfo studentInfoExist = studentInfoRepository.getStudentInfoByApartmentCodeAndChamberCodeAndBedId(
                studentInfo.getApartmentCode(),studentInfo.getChamberCode(),studentInfo.getBedId()
        );
        //如果不存在，进行添加
        //存在，修改
        if (studentInfoExist!=null) {
            studentInfoExist.setStatus(1);
            studentInfoExist.setUpdateTime(new Date());
            studentInfoExist.setAmountStatus(1);
            studentInfoExist.setIsDeleted(0);
            studentInfoExist.setTitle(studentInfo.getTitle());
            studentInfoRepository.save(studentInfo);
        }else {
            studentInfo.setStatus(1);
            studentInfo.setCreateTime(new Date());
            studentInfo.setUpdateTime(new Date());
            studentInfo.setAmountStatus(1);
            studentInfo.setIsDeleted(0);
            studentInfoRepository.save(studentInfo);
        }
    }

    @Override
    public Result addStudent(String bedId, StudentDetail studentDetail) {
        StudentInfo studentInfoExist = studentInfoRepository.getStudentInfoById(bedId);
        System.out.println("studentCode:"+studentDetail.getCertificatesNo());
        StudentInfo stuCodeExist = studentInfoRepository.getStudentInfoByStuCode(studentDetail.getCertificatesNo());
        if (stuCodeExist!=null){
            return Result.fail("该学生已入住");
        }
        if (studentInfoExist!=null){
            studentInfoExist.setStuCode(studentDetail.getCertificatesNo());
            studentInfoExist.setStuName(studentDetail.getName());
            studentInfoExist.setPhone(studentDetail.getPhone());
            studentInfoExist.setStatus(0);
            studentInfoRepository.save(studentInfoExist);
        }
        return Result.ok();
    }

    @Override
    public void deleteStudent(String bedId) {
        StudentInfo studentInfoExist = studentInfoRepository.getStudentInfoById(bedId);
        if (studentInfoExist!=null){
            studentInfoExist.setStatus(1);
            studentInfoExist.setStuName("");
            studentInfoExist.setPhone("");
            studentInfoExist.setStuCode("");
            studentInfoRepository.save(studentInfoExist);
        }
    }

    @Override
    public void removeChamber(String bedId) {
        StudentInfo studentInfoExist = studentInfoRepository.getStudentInfoById(bedId);
        if (studentInfoExist!=null){
            studentInfoExist.setIsDeleted(1);
            studentInfoExist.setStatus(1);
            studentInfoExist.setStuName("");
            studentInfoExist.setPhone("");
            studentInfoExist.setStuCode("");
            studentInfoRepository.save(studentInfoExist);
        }
    }

    @Override
    public void remove(String apartmentCode, String bedId) {
        //判断是否存在
        StudentInfo studentInfo = studentInfoRepository.getStudentInfoByApartmentCodeAndId(apartmentCode, bedId);
        if (studentInfo!=null) {
        //调用方法
            studentInfo.setIsDeleted(1);
//            studentInfoRepository.deleteById(studentInfo.getId());
            studentInfoRepository.save(studentInfo);
    }
}

    @Override
    public Page<StudentInfo> findPageStudentInfo(int page, int limit, StudentInfoQueryVo studentInfoQueryVo) {
        //创建pageable 设置当前页and记录数
        //0 为第一页
        Pageable pageable = PageRequest.of(page-1,limit);
        //创建Ex对象,传入对象

        //StudentInfo对象

        StudentInfo studentInfo = new StudentInfo();
        BeanUtils.copyProperties(studentInfoQueryVo,studentInfo);
        System.out.println("stuQuery:"+studentInfoQueryVo.toString());
        System.out.println("stuInfo:"+studentInfo.toString());
        studentInfo.setIsDeleted(0);
        studentInfo.setStatus(1);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//模糊查找
                .withIgnoreCase(true);//忽略大小写
        Example<StudentInfo> example = Example.of(studentInfo,matcher);

        Page<StudentInfo> all = studentInfoRepository.findAll(example, pageable);
        System.out.println("all:"+all.getContent().toString());
        return all;
    }

    @Override
    public List<StudentInfo> getDetailStudentInfo(String apartmentCode, String chamberCode) {
        List<StudentInfo> studentInfos= studentInfoRepository.findStudentInfoByApartmentCodeAndChamberCode(apartmentCode,chamberCode);
        studentInfos.stream().forEach(item->{
            this.packageStudentInfo(item);
        });
        return studentInfos;
    }

    @Override
    public Result addStudentByStuCodeAndName(List<UserInfoVo> userInfoVos) {

        for (UserInfoVo userInfoVo : userInfoVos){
            StudentInfo studentInfoExist = studentInfoRepository.getStudentInfoById(userInfoVo.getBedId());
            System.out.println("bedId:"+userInfoVo.getBedId());
            StudentInfo stuCodeExist = studentInfoRepository.getStudentInfoByStuCode(userInfoVo.getStuCode());
            if (stuCodeExist!=null){
                return Result.fail(userInfoVo.getStuCode()+"_"+userInfoVo.getName()+":该学生已入住");
            }
            if (studentInfoExist!=null){
                studentInfoExist.setStuCode(userInfoVo.getStuCode());
                studentInfoExist.setStuName(userInfoVo.getName());

                studentInfoExist.setStatus(0);
                studentInfoRepository.save(studentInfoExist);
            }
        }
        return Result.ok();

    }

    private void packageStudentInfo(StudentInfo studentInfo) {
        //设置宿舍名称
        studentInfo.getParam().put("apartmentName",apartmentService.getApartmentName(studentInfo.getApartmentCode()));
        //设置寝室名称
        studentInfo.getParam().put("chamberName",chamberService.getChamberName(studentInfo.getApartmentCode(),studentInfo.getChamberCode()));

    }

}
